package com.example.lotterysystem.controller;

import com.example.lotterysystem.common.pojo.CommonResult;
import com.example.lotterysystem.common.utils.JacksonUtil;
import com.example.lotterysystem.controller.param.DrawPrizeParam;
import com.example.lotterysystem.controller.param.ShowWinningRecordsParam;
import com.example.lotterysystem.controller.result.WinningRecordResult;
import com.example.lotterysystem.service.DrawPrizeService;
import com.example.lotterysystem.service.dto.WinningRecordDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


@RestController
public class DrawPrizeController {
    private final static Logger logger = LoggerFactory.getLogger(DrawPrizeController.class);

    @Autowired
    private DrawPrizeService drawPrizeService;

    @RequestMapping("/drawPrize")
    // 定义一个处理请求的方法，用于抽奖
    public CommonResult<Boolean>drawPrize(
            @RequestBody
            // 使用RequestBody注解，表示请求体中的数据将被转换为DrawPrizeParam对象
            @Validated DrawPrizeParam param){
        // 打印请求参数
        logger.info("drawPrize param:{}",param);
        // 调用drawPrizeService的drawPrize方法，传入请求参数
        drawPrizeService.drawPrize(param);
        // 返回一个成功的CommonResult对象，表示抽奖成功
return CommonResult.success(true);
    }

    @RequestMapping("/winning-records/show")
    public CommonResult<List<WinningRecordResult>>showWinningRecords(
            @Validated @RequestBody ShowWinningRecordsParam param){
logger.info("showWinningRecords param:{}",
        JacksonUtil.writeValueAsString(param));
        List<WinningRecordDTO> winningRecordDTOS=drawPrizeService.getRecords(param);
        logger.info("中奖记录返回结果：{}",
        JacksonUtil.writeValueAsString(winningRecordDTOS));
        return CommonResult.success(
                convertToWinningRecordResult(winningRecordDTOS));
    }

    private List<WinningRecordResult> convertToWinningRecordResult(
            List<WinningRecordDTO> winningRecordDTOS) {
        if (CollectionUtils.isEmpty(winningRecordDTOS)) {
            return Arrays.asList();
        }
        return winningRecordDTOS.stream()
                .map(winningRecordDTO -> {
                    WinningRecordResult result = new WinningRecordResult();
                    result.setPrizeName(winningRecordDTO.getPrizeName());
                    result.setPrizeTier(winningRecordDTO.getPrizeTiers().getMessage());
                    result.setWinnerId(winningRecordDTO.getWinnerId());
                    result.setWinnerName(winningRecordDTO.getWinnerName());
                    result.setWinningTime(winningRecordDTO.getWinningTime());
                    return result;
                }).collect(Collectors.toList());
    }
}
